import wsgiref.handlers
from google.appengine.ext import webapp

import Timegine

class Gadget( webapp.RequestHandler ):

  def get( self ):
    self.response.headers['Content-Type'] = 'application/xml'

    zones = Timegine.obtain_zones()
    option_str = ''

    for z in zones:
      full = z[1]

      if len(full) > 20:
        full = full[:20] + '...'

      option_str += \
        '<option value="%(value)s">%(abbr)s - %(full)s</option>\n' % {
          'value'   : z[2]
        , 'abbr'    : z[0]
        , 'full'    : full
      }

    self.response.out.write(
      r'''
      <?xml version="1.0" encoding="UTF-8"?>
        <Module>
        <ModulePrefs
          title="Yet Another Time Converter"
          description="Yet Another Time Converter - Misc-App Collection, Powered by Google App Engine"
          author="mr.kschan"
          author_email="Mr.KSChan+Misc-App@Gmail.com"
          author_location="hk"
          author_affiliation="Misc-App Collection"
          author_link="http://mr.kschan.googlepages.com/"
          title_url="http://code.google.com/p/misc-app/"
          height="300" />
        <Content type="html">
        <![CDATA[

          <style>
          div { font-size: 10pt; }
          input { font-size: 10pt; height: 22px; }
          select { font-size: 10pt; height: 22px; }
          button { font-size: 10pt; height: 22px; }
          pre { font-size: 10pt; }
          </style>

          <div>

          <form id="_form" action="#">

          Date - <input id="date" name ="date" type="text" />(dd/mm/yyyy)<br/>
          Time - <input id="time" name="time" type="text"  />(hh:mm)<br/>

          <br/>

          From -
          <select id="from-zone" name="from-zone">
          %(options)s
          </select>
          <br/>

          To -
          <select id="to-zone" name="to-zone">
          %(options)s
          </select>
          <br/>

          <br/>

          <button type="button" onclick="convert()">Convert!</button>

          </form>

          <pre id="result"
            style="padding: 3px; height: 50px; border: 1px solid #cccccc;">
          </pre>

          </div>

          <a href="http://appengine.google.com/" target="_blank"><img
          src="http://code.google.com/appengine/images/appengine-silver-120x30.gif"
          alt="Powered by Google App Engine" style="border:none;" /></a>

          <script type="text/javascript" language="javascript">
          var d     = new Date();
          var date  = ( ( d.getDate() > 9 )? '' : '0' ) + d.getDate();
          var month = ( ( d.getMonth() > 8 )? '' : '0' ) + (d.getMonth() + 1);
          var year  = d.getFullYear();
          var hour  = ( ( d.getHours() > 9 )? '' : '0' ) + d.getHours();
          var minute= ( ( d.getMinutes() > 9 )? '' : '0' ) + d.getMinutes();

          _gel("date").value = date + '/' + month + '/' + year;
          _gel("time").value = hour + ':' + minute;

          function convert() {

            var form = _gel('_form');
            var params =
                'from-zone=' + _gel('from-zone').value
              + '&to-zone=' + _gel('to-zone').value
              + '&date=' + _gel('date').value
              + '&time=' + _gel('time').value;

            _IG_FetchContent(
                  'http://misc-app.appspot.com/'
                + 'time-converter/convert?'
                + params
              , function (responseText) {

                var t = responseText.replace( /\( /g, '(+' );
                var result = _gel('result');

                if ( /MSIE (\d+\.\d+);/.test( navigator.userAgent ) ) {

                  result.outerHTML = '<pre id="result"'
                    + 'style="padding: 3px;'
                    + 'height: 50px;'
                    + 'border: 1px solid #cccccc;">'
                    + t + '</pre>';

                } else {

                  result.innerHTML = t;
                }

              }
            );
          }
          </script>

        ]]>
        </Content>
        </Module>
      '''.strip() % {
          'options' : option_str
      }
    )
  # end of Gadget.get

# end of class Gadget
